<?php

/**
 * 微信好友关系链 和 好友发消息
 * @author jimzyliang
 * @version 1.0.0 2019/07/08
 * @since
 *
 */

namespace Lib\Paas;

class Relation extends Base
{
    public function __construct($sData = [])
    {
        parent::__construct($sData);
    }

    /**
     * 微信好友关系链
     * @param int $taskId 产品提供，一次活动申请一个，限量都控制在taskId里
     * @param str $wxappName 产品提供，需产品申请。
     * 
     * @return array
     * [
     *      'ret' => '0-成功，其他失败',
     *      'msg' => '接口返回的信息',
     *      'errNo' => '错误码',
     *      'sSerial' => '流水号',
     *      ['data'] => [
     *          "error":{
     *               "code":0,  //失败则-90001
     *               "message":"ok" 
     *           },
     *          'result' => {
     *              'model_id' => 3 ,数字
     *              'send_list' => '已发送列表，常为空数组，没啥用',
     *              'friend_list' => [
     *                  {
     *                      "sopenid"=>"ssFm5w-Irxxxxxxxxxxxxxxx",
     *                      "head_img_url"=>"http://wx.qlogo.cn/mmopen/vi_32/sWJxxxxxxxxxSKNys6m3BJSMDUXRJpEnp5lqAiaUw/0",
     *                      "nick_name"=>"青dd裙",
     *                      "score"=>0.0989999994635582,
     *                      "tag":1
     *                  },
     *                  {
     *                  }
     *              ]
     *          }
     *      ],
     * ]
     */
    public function getWxRelation(array $data)
    {
        if (!$data['wxappName']) {
            return $this->getRetData('-1', 'wxappName不合法！');
        }

        if ($data['taskId'] && is_numeric($data['taskId'])) {
        } else {
            return $this->getRetData('-2', 'taskId不合法！');
        }

        $url = $this->getPaasRequestUrl('user.wxrelation');

        $res = $this->http->post($url, http_build_query($data));

        recordLog('请求自系统微信关系链模块-查询好友列表返回值：' . var_export($res, true), __FILE__, __LINE__);

        if ($res['ret'] != 0) {
            $this->tglogReport('框架请求ULink-Paas出现异常，curl_errno：' . $res['ret'] . '，curl_error：' . $res['msg'], 'module.query');
            return $this->getRetData(CURL_ERROR_NO, '系统繁忙，请稍后再试！');
        }
        $res = $res['data'];

        if ($res['ret'] != 0) {
            return $this->getRetData($res['ret'], '系统繁忙，请稍后再试！');
        } else {
            return $this->getRetData('0', 'success', ['data' => $res['data']]);
        }
    }

    /**
     * 给微信好友发消息
     * @param array $data =
     * [
     *       'taskId' => 产品提供, 频率控制都要针对它
     *       'wxappName' => 产品提供,
     *       'toUser' => 上面查出的加密openid,
     *       'modelId' => 上面查出的modelId,
     *       'msgType' => 消息类型, link分享消息 / openid
     *       'msgTitle' => 标题
     *       'msgDesc' => 描述
     *       'msgUrl' => 分享消息打开后的链接
     *       'msgThumbUrl' => 分享右边的小icone
     *]
     * @return array
     * [
     *      'ret' => '0-成功，其他失败',
     *      'msg' => '接口返回的信息',
     *      'errNo' => '错误码',
     *      'sSerial' => '流水号',
     *      ['data'] => [
     *          "error":{
     *               "code":0,  //失败则-90004
     *               "message":"ok" 
     *           },
     *      ],
     * ]
     */
    public function sendWxMsg(array $data)
    {
        //基础
        if (!$data['wxappName']) {
            return $this->getRetData('-1', 'wxappName不合法！');
        }

        if ($data['taskId'] && is_numeric($data['taskId'])) {
        } else {
            return $this->getRetData('-2', 'taskId不合法！');
        }

        if (!$data['toUser']) {
            return $this->getRetData('-3', '好友信息不合法！');
        }

        if (!$data['msgTitle'] || !$data['msgThumbUrl']) {
            return $this->getRetData('-4', '消息title或分享图片不合法！');
        }

        if (!$data['msgUrl']) {
            return $this->getRetData('-5', '消息跳转地址不合法！');
        }

        //发送微信信息
        $url = $this->getPaasRequestUrl('user.wxrelation', ['dosendmsg' => 1]);

        $res = $this->http->post($url, http_build_query($data));

        recordLog('请求自系统微信关系链模块--发送好友消息返回值：' . var_export($res, true), __FILE__, __LINE__);

        if ($res['ret'] != 0) {
            $this->tglogReport('框架请求ULink-Paas出现异常，curl_errno：' . $res['ret'] . '，curl_error：' . $res['msg'], 'module.query');
            return $this->getRetData(CURL_ERROR_NO, '系统繁忙，请稍后再试！');
        }
        $res = $res['data'];

        if ($res['ret'] != 0) {
            return $this->getRetData($res['ret'], '系统繁忙，请稍后再试！');
        } else {
            return $this->getRetData('0', 'success', ['data' => $res['data']]);
        }
    }

    /**
     * 由于是msdk接口 - 仅限游戏内入口
     * 获取qq好友关系链
     * [
     *      'appid' => $appid,
     *      'msdkkey' => $msdkkey,
     *      'count' => $count,
     * ]
     */
    public function getQqRelation(array $data)
    {
        if (!$data['appid']) {
            return $this->getRetData('-1', 'appid不合法！');
        }
        if (!$data['msdkkey']) {
            return $this->getRetData('-2', 'msdkkey不合法！');
        }
        if ($data['count'] > 0 && $data['count'] <= 20) {
        } else {
            return $this->getRetData('-3', 'count不合法！');
        }

        $url = $this->getPaasRequestUrl('user.qqrelation');

        $res = $this->http->post($url, http_build_query($data));

        recordLog('请求自系统QQ关系链模块--拉取好友列表返回值：' . var_export($res, true), __FILE__, __LINE__);

        if ($res['ret'] != 0) {
            $this->tglogReport('框架请求ULink-Paas出现异常，curl_errno：' . $res['ret'] . '，curl_error：' . $res['msg'], 'module.query');
            return $this->getRetData(CURL_ERROR_NO, '系统繁忙，请稍后再试！');
        }
        $res = $res['data'];

        if ($res['ret'] != 0) {
            return $this->getRetData($res['ret'], '系统繁忙，请稍后再试！');
        } else {
            return $this->getRetData('0', 'success', ['data' => $res['data']]);
        }
    }

    /**
     * 由于是msdk接口 - 仅限游戏内入口
     * 给qq好友发送消息
     * [
     *      'appid' => $appid,
     *      'msdkkey' => $msdkkey,
     *      'fopenids' => $fopenids,
     *      'image_url' => $image_url,
     *      'title' => $title,
     *      'target_url' => $target_url,
     *      'userip' => $userip,
     * 
     *      'summary' => $summary, //可选 
     *      'game_tag' => $game_tag, //可选 默认为MSG_INVITE_ARK
     * ]
     */
    public function sendQqMsg(array $data)
    {
        if (!$data['appid']) {
            return $this->getRetData('-1', 'appid不合法！');
        }

        if (!$data['msdkkey']) {
            return $this->getRetData('-2', 'msdkkey不合法！');
        }

        if (!$data['fopenids']) {
            return $this->getRetData('-3', 'fopenids不合法！');
        }

        if (!$data['image_url']) {
            return $this->getRetData('-4', 'image_url不合法！');
        }

        if (!$data['title']) {
            return $this->getRetData('-5', 'title不合法！');
        }

        if (!$data['target_url']) {
            return $this->getRetData('-6', 'target_url不合法！');
        }

        if (!$data['userip']) {
            return $this->getRetData('-7', 'userip不合法！');
        }

        $url = $this->getPaasRequestUrl('user.qqrelation', ['dosendmsg' => 1]);

        $res = $this->http->post($url, http_build_query($data));

        recordLog('请求自系统微信关系链模块--发送好友消息返回值：' . var_export($res, true), __FILE__, __LINE__);

        if ($res['ret'] != 0) {
            $this->tglogReport('框架请求ULink-Paas出现异常，curl_errno：' . $res['ret'] . '，curl_error：' . $res['msg'], 'module.query');
            return $this->getRetData(CURL_ERROR_NO, '系统繁忙，请稍后再试！');
        }
        $res = $res['data'];

        if ($res['ret'] != 0) {
            return $this->getRetData($res['ret'], '系统繁忙，请稍后再试！');
        } else {
            return $this->getRetData('0', 'success', ['data' => $res['data']]);
        }
    }

    /**
     * msdk V5 新版本 - 拉取qq好友关系链
     * [
     *      'os' => $os,         //string 必填 操作系统标示1 - Android, 2 - iOS, 3 - Web，
     *      'gameid' => $gameid, //string 必填 MSDK分配的游戏id
     *      'channelid' => $channelid, // int 【必填】 登录渠道id 1 - 微信(WeChat)， 2 - 手Q(mqq)，3 - 游客(Guest)
     *      'MSDK_SERVER_KEY' => $MSDK_SERVER_KEY, //产品提供，1 - 后台，签名密钥使用 MSDK_SERVER_KEY 的值
     *      'count' => $count,         //int	[手Q必填]期待拉取好友的个数，建议填写 20（不要超过 20）
     * ]
     */
    public function getQqRelationV5(array $data)
    {

        if (!$data['os'] || !in_array($data['os'], ['1', '2', '3'])) {
            return $this->getRetData('-1', 'os不合法！');
        }

        if (!$data['gameid']) {
            return $this->getRetData('-2', 'gameid不合法！');
        }

        if (!$data['channelid'] || !in_array($data['channelid'], [1, 2, 3])) {
            return $this->getRetData('-3', 'channelid不合法！');
        }

        if (!$data['MSDK_SERVER_KEY']) {
            return $this->getRetData('-4', 'MSDK_SERVER_KEY不合法！');
        }

        if ($data['count'] > 0 && $data['count'] <= 20) {
        } else {
            return $this->getRetData('-5', 'count不合法！');
        }

        $url = $this->getPaasRequestUrl('user.qqrelation', ['isMsdkV5' => 1]);

        $res = $this->http->post($url, http_build_query($data));

        recordLog('请求自系统QQ关系链V5模块--拉取好友列表返回值：' . var_export($res, true), __FILE__, __LINE__);

        if ($res['ret'] != 0) {
            $this->tglogReport('框架请求ULink-Paas出现异常，curl_errno：' . $res['ret'] . '，curl_error：' . $res['msg'], 'module.query');
            return $this->getRetData(CURL_ERROR_NO, '系统繁忙，请稍后再试！');
        }
        $res = $res['data'];

        if ($res['ret'] != 0) {
            return $this->getRetData($res['ret'], '系统繁忙，请稍后再试！');
        } else {
            return $this->getRetData('0', 'success', ['data' => $res['data']]);
        }
    }

    /**
     * msdk V5 新版本 - 发送qq好友消息
     * [
     *      'os' => $os,         //string 必填 操作系统标示1 - Android, 2 - iOS, 3 - Web，
     *      'gameid' => $gameid, //string 必填 MSDK分配的游戏id
     *      'channelid' => $channelid, // int 【必填】 登录渠道id 1 - 微信(WeChat)， 2 - 手Q(mqq)，3 - 游客(Guest)
     *      'MSDK_SERVER_KEY' => $MSDK_SERVER_KEY, //产品提供，1 - 后台，签名密钥使用 MSDK_SERVER_KEY 的值
     *      'sopenid' => $sopenid,  //【邀请未注册好友必填】未注册好友 sopenid
     *      'title' => $title, 
     *      'desc' => $desc, 
     *      'channel_info' => array('target_url'=>$target_url),  //分享的url，是数组，比较特别
     *      'image_url' => $image_url, 
     * 
     *      'game_tag' => $game_tag, //可选， 默认 MSG_INVITE_ARK
     * ]
     */
    public function sendQqMsgV5(array $data)
    {
        if (!$data['os'] || !in_array($data['os'], ['1', '2', '3'])) {
            return $this->getRetData('-1', 'os不合法！');
        }

        if (!$data['gameid']) {
            return $this->getRetData('-2', 'gameid不合法！');
        }

        if (!$data['channelid'] || !in_array($data['channelid'], [1, 2, 3])) {
            return $this->getRetData('-3', 'channelid不合法！');
        }

        if (!$data['MSDK_SERVER_KEY']) {
            return $this->getRetData('-4', 'MSDK_SERVER_KEY不合法！');
        }

        if (!$data['sopenid']) {
            return $this->getRetData('-5', 'sopenid不合法！');
        }

        if (!$data['title']) {
            return $this->getRetData('-6', 'title不合法！');
        }

        if (!$data['desc']) {
            return $this->getRetData('-7', 'desc不合法！');
        }

        if (!$data['channel_info']['target_url']) {
            return $this->getRetData('-8', 'target_url不合法！');
        }

        if (!$data['image_url']) {
            return $this->getRetData('-9', 'image_url不合法！');
        }

        $data['dosendmsg'] = 1;
        $data['isMsdkV5'] = 1;

        $url = $this->getPaasRequestUrl('user.qqrelation', ['dosendmsg' => 1, 'isMsdkV5' => 1]);

        $res = $this->http->post($url, http_build_query($data));

        recordLog('请求自系统微信关系链模块--发送好友消息返回值：' . var_export($res, true), __FILE__, __LINE__);

        if ($res['ret'] != 0) {
            $this->tglogReport('框架请求ULink-Paas出现异常，curl_errno：' . $res['ret'] . '，curl_error：' . $res['msg'], 'module.query');
            return $this->getRetData(CURL_ERROR_NO, '系统繁忙，请稍后再试！');
        }
        $res = $res['data'];

        if ($res['ret'] != 0) {
            return $this->getRetData($res['ret'], '系统繁忙，请稍后再试！');
        } else {
            return $this->getRetData('0', 'success', ['data' => $res['data']]);
        }
    }
}
